package com.adwhirl;
/*
* AdWhirlActivity.java - Extension class to the
* AdWhirl 3.0 Android SDK demonstrating control
* methodology using Activity event callbacks to
* relay state information to qualified receivers
* and initiate response action.   
*  
* (c)2011 - Kent Butler / KB Systems Design 
* 
* May be freely distributed and modified. Provided for
* educational purposes on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied.
* 
**/
import com.adwhirl.AdWhirlLayout;
import com.adwhirl.util.AdWhirlUtil;

import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
/**
 * Activity extension class overriding event
 * callbacks to send notifications to AdWhirlLayouts.<br><br>
 * 
 * Overrides onPause(), onResume() and onDestroy()
 * to propagate these events to a callback method
 * for all adWhirlLayout objects managed by the
 * WindowManager handling the activity.
 * 
 * @author Kent Butler, 
 *         April 20, 2011
 *         Revision:3(review)
 */
public class AdWhirlActivity extends Activity{
	  // KGB - IDs for Messages dispatched from AdWhirlActivity to AdWhirlLayouts //
	  public static final int MSG_ONPAUSE = 1;
	  public static final int MSG_ONRESUME = 2;
	  public static final int MSG_ONDESTROY = 3;
	  
	/** Turn on/off console messaging */
	public static boolean consoleSpam = false;
	
	@Override
	public void onPause(){
		super.onPause();
		if(consoleSpam) Log.w(AdWhirlUtil.ADWHIRL,"System OnPause: System event dispatched!");		
		doMessageAdWhirl(MSG_ONPAUSE);
	}
	@Override
	public void onDestroy(){
		super.onDestroy();
		if(consoleSpam) Log.w(AdWhirlUtil.ADWHIRL,"System OnDestroy: System event dispatched!");
		doMessageAdWhirl(MSG_ONDESTROY);	
	}
	@Override
	public void onResume(){
		super.onResume();
		if(consoleSpam) Log.w(AdWhirlUtil.ADWHIRL,"System OnResume: System event dispatched!");
		doMessageAdWhirl(MSG_ONRESUME);
	}
	
	private void doMessageAdWhirl(int message){
		ViewGroup vg = (ViewGroup) this.getWindow().getDecorView().getRootView();
		int c=vg.getChildCount();
		if(consoleSpam) Log.i(AdWhirlUtil.ADWHIRL, "System Event: Dispatch message to all AdWhirlLayouts.");
		for(int k=0;k<c;k++){
			messageViewsInGroup(vg, message);
		}
	}
	
	private void messageViewsInGroup(ViewGroup group, int message){
		int cnt = group.getChildCount();		
		for(int k=0;k<cnt;k++){
			View v = group.getChildAt(k);
			if(v instanceof AdWhirlLayout){
				messageAdWhirlLayout( (AdWhirlLayout) v, message);
			} else if (v instanceof ViewGroup){
				messageViewsInGroup( (ViewGroup)v, message);
			}
		}
	}
	protected void messageAdWhirlLayout(AdWhirlLayout layout, int message){
		if(consoleSpam){
			String tmp=null;			
			if(message == MSG_ONPAUSE){
				tmp="ONPAUSE";
			} else if(message == MSG_ONRESUME){
				tmp="ONRESUME";
			} else if(message == MSG_ONDESTROY){
				tmp="ONDESTROY";
			} else {
				tmp="Unknown Message:"+message;
			}			
			Log.i(AdWhirlUtil.ADWHIRL,"System Event: Dispatching message to AdWhirlLayout:"+tmp);
		}
		layout.handleActivityMessage( message, this.isFinishing() );
	}
}
